package com.leyou.item.service;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.LevelUtil;
import com.leyou.item.dao.CategoryMapper;
import com.leyou.item.dto.CategoryLevelDto;
import com.leyou.item.pojo.Category;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author:yanjizhw@163.com
 * @date:2019-09-23 14:44
 * 描述:
 * @since:1.0.0
 */
@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    public List<Category> queryCategoryList() {

        List<Category> list = categoryMapper.selectCategoryTree();

        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        return list;
    }

    public List<CategoryLevelDto> queryCategoryTree() {

        List<Category> categories = categoryMapper.selectAll();

        List<CategoryLevelDto> dtoList = new ArrayList<>();

        for (Category category : categories){
            dtoList.add(CategoryLevelDto.adapt(category));
        }

        return categoryListToTree(dtoList);
    }

    public List<CategoryLevelDto> categoryListToTree(List<CategoryLevelDto> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return new ArrayList();
        }
        // level -> [aclmodule1, aclmodule2, ...] Map<String, List<Object>>
        Multimap<String, CategoryLevelDto> levelCategoryMap = ArrayListMultimap.create();
        List<CategoryLevelDto> rootList = new ArrayList();

        for (CategoryLevelDto dto : dtoList) {
            levelCategoryMap.put(dto.getLevel(), dto);
            if (LevelUtil.ROOT.equals(dto.getLevel())) {
                rootList.add(dto);
            }
        }
        Collections.sort(rootList, categorySeqComparator);
        transformCategoryTree(rootList, LevelUtil.ROOT, levelCategoryMap);
        return rootList;
    }

    public void transformCategoryTree(List<CategoryLevelDto> dtoList, String level, Multimap<String, CategoryLevelDto> levelCategoryMap) {
        for (int i = 0; i < dtoList.size(); i++) {
            CategoryLevelDto dto = dtoList.get(i);
            String nextLevel = LevelUtil.calculateLevel(level, dto.getId());
            List<CategoryLevelDto> tempList = (List<CategoryLevelDto>) levelCategoryMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempList)) {
                Collections.sort(tempList, categorySeqComparator);
                dto.setCategoryList(tempList);
                transformCategoryTree(tempList, nextLevel, levelCategoryMap);
            }
        }
    }



    public Comparator<CategoryLevelDto> categorySeqComparator = new Comparator<CategoryLevelDto>() {
        public int compare(CategoryLevelDto o1, CategoryLevelDto o2) {
            return o1.getSort() - o2.getSort();
        }
    };

}
